package com.dh.project.controller;

import com.dh.project.model.CustomerAddress;
import com.dh.project.model.CustomerInfo;
import com.dh.project.model.PageInfo;
import com.dh.project.model.PageModel;
import com.dh.project.other.ModelRes;
import com.dh.project.service.CustomerAddressService;
import com.dh.project.service.CustomerInfoService;
import com.dh.project.utils.PageUtil;
import com.dh.project.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/** 客户操作Controller
 * Created by admin on 2017/3/29.
 */
@Controller
public class CustomerController {

    private CustomerInfoService customerInfoService;

    private CustomerAddressService customerAddressService;

    @Autowired
    public CustomerController(CustomerInfoService customerInfoService,CustomerAddressService customerAddressService){
        this.customerInfoService = customerInfoService;
        this.customerAddressService = customerAddressService;
    }

//    @RequestMapping(value = "getCustomerList",produces = "text/html;charset=utf-8")
//    public String getCustomerList(){
//        Map map = new HashMap();
//        List<CustomerInfo> customerInfos = customerInfoService.selectByMap(map);
//        return "";
//    }

    @RequestMapping(value = "testController",produces = "text/html;charset=utf-8")
    public String testController(Model model){
        Map map = new HashMap();
        List<CustomerInfo> customerInfos = customerInfoService.selectByMap(map);
        model.addAttribute("customerInfos",customerInfos);
        return "customer_info";
    }

    /**
     * 客户信息列表
     * @param model
     * @return
     */
    @RequestMapping(value = "selectCustomerList",produces = "text/html;charset=utf-8")
    public String selectCustomerList(CustomerInfo customerInfo ,int page,Model model){
        Map map = new HashMap();
        map.put("customer_id",customerInfo.getCustomer_id());
        map.put("customer_name",customerInfo.getCustomer_name());

        map.put("page_start",(page-1)*10);
        map.put("page_end",10);

        List<CustomerInfo> customerInfos = customerInfoService.selectCustomerListByMap(map);



        PageModel pageModel = customerInfoService.selectCustomerListByMapCount(map);
        PageInfo pageInfo = PageUtil.getPageInfo(pageModel,page,10);
        pageInfo.setPage_href(PageUtil.getPageHref(customerInfo,2));
        pageInfo.setPage_index(page);
        pageInfo.setPage_count(PageUtil.getPageCount(pageModel.getPage_index(),10));

        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("customerInfos",customerInfos);
        model.addAttribute("customerInfo",customerInfo);
        return "customer_info";
    }

    /**
     * 修改客户信息
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "updateCustomerInfo",produces = "text/html;charset=utf-8")
    public Object updateCustomerInfo(CustomerInfo customerInfo1){
        CustomerInfo customerInfo = customerInfoService.selectById(Long.valueOf(customerInfo1.getCustomer_id()));
        if (!StringUtil.isBlank(customerInfo1.getCustomer_name())){
            customerInfo.setCustomer_name(customerInfo1.getCustomer_name());
        }
        if (!StringUtil.isBlank(customerInfo1.getCustomer_phone())){
            Map map = new HashMap();
            map.put("customer_phone",customerInfo1.getCustomer_phone());
            List<CustomerInfo> customerInfos = customerInfoService.selectByMap(map);
            if (customerInfos.size() > 1 || customerInfos.get(0).getCustomer_id() != customerInfo1.getCustomer_id()){
                return new ModelRes(ModelRes.Status.ERROR,"该手机号吗已存在，修改失败!");
            }else {
                customerInfo.setCustomer_phone(customerInfo1.getCustomer_phone());
            }
        }
        if (customerInfo1.getIs_freeze() != null){
            customerInfo.setIs_freeze(customerInfo1.getIs_freeze());
        }
        boolean isSuccess = customerInfoService.updateById(customerInfo);
        if (isSuccess){
            return new ModelRes();
        }else {
            return new ModelRes(ModelRes.Status.ERROR,"修改失败!");
        }
    }


    /**
     * 查询客户信息详情
     * @param customer_id
     * @return
     */
    @RequestMapping(value = "selectCustomerDetail",produces = "text/html;charset=utf-8")
    public String selectCustomerDetail(int customer_id,Model model){
        CustomerInfo customerInfo = customerInfoService.selectById(Long.valueOf(customer_id));
        Map map = new HashMap();
        map.put("customer_id",customer_id);
        List<CustomerAddress> customerAddresses = customerAddressService.selectByMap(map);

        model.addAttribute("customerInfo",customerInfo);
        model.addAttribute("customerAddresses",customerAddresses);
        return "customer_detail";
    }

}
